# -*- encoding: utf-8 -*-
from django import forms
from app import models
from django.forms.widgets import HiddenInput
from django.utils.safestring import mark_safe

GENERO_CHOICES = [(genero.id, genero.nombre_largo) for genero in models.Generos.objects.all()]
ROL_CHOICES = [('reg','Registrado'),('admin','Administrador')]
CRITICA_VOTA = [('1','1'),('2','2'),('3','3'),('4','4'),('5','5')]

class HorizontalRadioRenderer(forms.RadioSelect.renderer):
    def render(self):
        return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))

class LoginForm(forms.Form):
    username = forms.CharField(label='Usuario')
    passwd = forms.CharField(label='Contraseña', widget=forms.PasswordInput)

class RegistroForm(forms.Form):
    username = forms.CharField(label='Nombre de usuario')
    first_name = forms.CharField(label='Nombre')
    last_name = forms.CharField(label='Apellidos')
    passwd1 = forms.CharField(label='Contraseña', widget=forms.PasswordInput)
    passwd2 = forms.CharField(label='Repita contraseña', widget=forms.PasswordInput)
    email = forms.EmailField()
    
    def clean(self):
        try:
            if self.cleaned_data['passwd1'] != self.cleaned_data['passwd2']:
                raise forms.ValidationError('Las contraseñas no coinciden')
        except KeyError:
            pass
        return self.cleaned_data

class ConfigForm(forms.Form):
    first_name = forms.CharField(label='Nombre')
    last_name = forms.CharField(label='Apellidos')
    passwd1 = forms.CharField(label='Contraseña', required=False, widget=forms.PasswordInput)
    passwd2 = forms.CharField(label='Repita contraseña', required=False, widget=forms.PasswordInput)
    email = forms.EmailField()
    
    def clean(self):
        try:
            if self.cleaned_data['passwd1'] != self.cleaned_data['passwd2']:
                raise forms.ValidationError('Las contraseñas no coinciden')
        except KeyError:
            pass
        return self.cleaned_data
    
class PeliculaAddForm(forms.Form):
    titulo = forms.CharField(label='Título')
    director = forms.CharField()
    actores = forms.CharField(widget=forms.Textarea, max_length=300)
    anyo = forms.CharField(label='Año')
    nacionalidad = forms.CharField()
    productora = forms.CharField()
    genero = forms.CharField(label='Género', widget=forms.Select(choices=GENERO_CHOICES))
    sinopsis = forms.CharField(widget=forms.Textarea, max_length=1500)
    url_imagen = forms.CharField(label='URL Imagen')
    
class CriticaAddForm(forms.Form):
    titulo = forms.CharField(label='Título')
    contenido = forms.CharField(widget=forms.Textarea, max_length=1500)
    nota = forms.CharField(widget=forms.RadioSelect(choices=CRITICA_VOTA, renderer=HorizontalRadioRenderer))
    valorada = forms.BooleanField(required=False)
    
    #comprobamos el valor de valorada. Si es True hacemos el campo nota no requerido para que la validacion no
    # lo tome en cuenta
    def __init__(self, data=None, *args, **kwargs):
        super(CriticaAddForm, self).__init__(data, *args, **kwargs)
        if self.fields['valorada']:
            self.fields['nota'].required = False

class CriticaModForm(forms.Form):
    titulo = forms.CharField(label='Título')
    contenido = forms.CharField(widget=forms.Textarea, max_length=1500)
    
class CriticaModUserForm(forms.Form):
    titulo = forms.CharField(label='Título', required=False)
    contenido = forms.CharField(widget=forms.Textarea, max_length=1500, required=False)
    nota = forms.CharField(widget=forms.RadioSelect(choices=CRITICA_VOTA, renderer=HorizontalRadioRenderer), required=False)

class AdminModUserForm(forms.Form):
    first_name = forms.CharField(label='Nombre')
    last_name = forms.CharField(label='Apellidos')
    passwd1 = forms.CharField(label='Contraseña', required=False, widget=forms.PasswordInput)
    passwd2 = forms.CharField(label='Repita contraseña', required=False, widget=forms.PasswordInput)
    email = forms.EmailField()
    rol = forms.CharField(widget=forms.Select(choices=ROL_CHOICES))
    
    def clean(self):
        try:
            if self.cleaned_data['passwd1'] != self.cleaned_data['passwd2']:
                raise forms.ValidationError('Las contraseñas no coinciden')
        except KeyError:
            pass
        return self.cleaned_data

class AdminAddUserForm(forms.Form):
    username = forms.CharField(label='Nombre de usuario')
    first_name = forms.CharField(label='Nombre')
    last_name = forms.CharField(label='Apellidos')
    passwd1 = forms.CharField(label='Contraseña', widget=forms.PasswordInput)
    passwd2 = forms.CharField(label='Repita contraseña', widget=forms.PasswordInput)
    email = forms.EmailField()
    rol = forms.CharField(widget=forms.Select(choices=ROL_CHOICES))
    
    def clean(self):
        try:
            if self.cleaned_data['passwd1'] != self.cleaned_data['passwd2']:
                raise forms.ValidationError('Las contraseñas no coinciden')
        except KeyError:
            pass
        return self.cleaned_data

    